package com.bicon.botu.mall.goods.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.goods.commons.GoodsConstants;
import com.bicon.botu.mall.goods.entity.Brand;
import com.bicon.botu.mall.goods.entity.Product;
import com.bicon.botu.mall.goods.mapper.BrandMapper;
import com.bicon.botu.mall.goods.service.IBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 品牌 Service 实现层
 *
 * @author 吕祥
 * @date 2017/7/27 10:56
 */
@Slf4j
@Service
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 新增品牌
     *
     * @param chinaName   中文名称
     * @param englishName 英文名称
     * @param logo        logo图标
     * @param state       是否启用(0 = 已启用， 1 = 未启用)
     * @param remark      品牌描述
     * @return
     */
    @Override
    public BaseResult add(String chinaName, String englishName, String logo, String state, String remark) {
        Brand brand = new Brand();
        if (chinaName.length() > 20 || englishName.length() > 20 || remark.length() > 50) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_400, GoodsConstants.RESPONSE_MESSAGE_400);
        }
        if (!state.equals(GoodsConstants.BRAND_ENABLED) && !state.equals(GoodsConstants.BRAND_NOT_ENABLED)) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_400, GoodsConstants.RESPONSE_MESSAGE_400);
        }
        brand.setChinaName(chinaName);
        brand.setEnglishName(englishName);
        brand.setLogo(logo);
        brand.setState(state);
        brand.setRemark(remark);
        brand.preInsert();
        boolean result = brand.insert();
        if (result) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_600, GoodsConstants.RESPONSE_MESSAGE_600);
        }
    }

    /**
     * 品牌修改
     *
     * @param id          品牌id
     * @param chinaName   中文名称
     * @param englishName 英文名称
     * @param logo        logo图标
     * @param state       是否启用(0 = 已启用， 1 = 未启用)
     * @param remark      品牌描述
     * @return
     */
    @Override
    public BaseResult updateById(String id, String chinaName, String englishName, String logo, String state, String remark) {
        if (chinaName.length() > 20 || englishName.length() > 20 || remark.length() > 50) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_400, GoodsConstants.RESPONSE_MESSAGE_400);
        }
        if (!state.equals(GoodsConstants.BRAND_ENABLED) && !state.equals(GoodsConstants.BRAND_NOT_ENABLED)) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_400, GoodsConstants.RESPONSE_MESSAGE_400);
        }
        Brand brand = new Brand().selectById(id);
        if (null != chinaName) {
            brand.setChinaName(chinaName);
        }
        if (null != englishName) {
            brand.setEnglishName(englishName);
        }
        if (null != logo) {
            brand.setLogo(logo);
        }
        if (null != state) {
            brand.setState(state);
        }
        if (null != remark) {
            brand.setRemark(remark);
        }
        brand.preUpdate();
        boolean result = brand.updateById();
        if (result) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_600, GoodsConstants.RESPONSE_MESSAGE_600);
        }
    }

    /**
     * 删除品牌
     *
     * @param id 品牌id
     * @return
     */
    @Override
    public BaseResult deleteById(String id) {
        Brand brand = new Brand().selectById(id);
        List<Product> productList = new Product().selectList(new EntityWrapper<Product>().eq("logic_delete",GoodsConstants.DEL_LOGIC_NORMAL)
                                                            .eq("brand_id", id));
        if (productList.size() > 0) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_600, "不可删除，品牌下有商品存在");
        }
        brand.preDelete();
        boolean result = brand.updateById();
        if (result) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_600, GoodsConstants.RESPONSE_MESSAGE_600);
        }
    }

    /**
     * 查询所有品牌
     *
     * @return 所有品牌信息
     */
    @Override
    public BaseResult findAll() {
        List<Brand> brandList = new Brand().selectList(new EntityWrapper().eq("logic_delete", GoodsConstants.DEL_LOGIC_NORMAL).orderBy("create_date", false));
        if (brandList != null) {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200, brandList);
        } else {
            return new BaseResult(GoodsConstants.RESPONSE_CODE_602, GoodsConstants.RESPONSE_MESSAGE_602);
        }
    }

    /**
     * 根据品牌名称 查询品牌
     *
     * @param brandName 品牌名称
     * @return
     */
    @Override
    public BaseResult findByBrandName(String brandName) {
        EntityWrapper<Brand> entityWrapper = new EntityWrapper<Brand>();
        entityWrapper.eq("logic_delete", GoodsConstants.DEL_LOGIC_NORMAL);
        //模糊查询中文名称
        entityWrapper.like("china_name", brandName);
        entityWrapper.or();
        entityWrapper.eq("logic_delete", GoodsConstants.DEL_LOGIC_NORMAL);
        //模糊查询英文名称
        entityWrapper.like("english_name", brandName);
        entityWrapper.orderBy("createDate", false);
        List<Brand> brand = new Brand().selectList(entityWrapper);
        return new BaseResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200, brand);
    }

    /**
     * 品牌 分页查询
     *
     * @param pageNo    页数
     * @param pageSize  页面数量
     * @return
     */
    @Override
    public BaseResult findPage(int pageNo, int pageSize) {
        Page<Brand> page = new Brand().selectPage(new Page<Brand>(pageNo, pageSize),
                                        new EntityWrapper<Brand>().eq("logic_delete", GoodsConstants.DEL_LOGIC_NORMAL).orderBy("create_date",false));
        return new BaseResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200, page);
    }

    /**
     * 根据分类path查询品牌
     *
     * @param path  分类path
     * @return
     */
    @Override
    public BaseResult findByClassPath(String path) {
        List<Brand> brandList = brandMapper.findByClassfication(path);
        return new BaseResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200, brandList);
    }
}
